﻿Public Class Account

    Private _Login As String
    Private _Password As String
    Private _Mail As String
    Private _AddressIP As String
    Private _Characters() As Character
    Private _Grade As Integer
    Private _CurrentSlot As Integer


    Public Sub New()

        _Login = String.Empty
        _Password = String.Empty

        ReDim _Characters(Settings.MaximumCharacters)

        For i As Integer = 1 To Settings.MaximumCharacters
            _Characters(i) = New Character
        Next

    End Sub

#Region "Properties"

    Public Property Login As String
        Get
            Return _Login
        End Get
        Set(ByVal value As String)
            _Login = value
        End Set
    End Property

    Public Property Password As String
        Get
            Return _Password
        End Get
        Set(ByVal value As String)
            _Password = value
        End Set
    End Property

    Public Property Mail As String
        Get
            Return _Mail
        End Get
        Set(ByVal value As String)
            _Mail = value
        End Set
    End Property

    Public Property AddressIP As String
        Get
            Return _AddressIP
        End Get
        Set(ByVal value As String)
            _AddressIP = value
        End Set
    End Property

    Public Property Character(ByVal i As Integer) As Character
        Get
            Return _Characters(i)
        End Get
        Set(ByVal value As Character)
            _Characters(i) = value
        End Set
    End Property

    Public Property Grade As Integer
        Get
            Return _Grade
        End Get
        Set(ByVal value As Integer)
            _Grade = value
        End Set
    End Property

    Public Property CurrentSlot As Integer
        Get
            Return _CurrentSlot
        End Get
        Set(ByVal value As Integer)
            _CurrentSlot = value
        End Set
    End Property

#End Region

    Public Sub LoadAccount(ByVal Login As String)

        Dim XmlDoc As New XmlDocument()

        Try

            XmlDoc.Load(AccountsPath & Login.Trim & Settings.DocsFormat)

            _Login = XmlDoc.SelectSingleNode("/Account/General/Login").InnerText
            _Password = XmlDoc.SelectSingleNode("/Account/General/Password").InnerText
            _AddressIP = XmlDoc.SelectSingleNode("/Account/General/AddressIP").InnerText
            _Mail = XmlDoc.SelectSingleNode("/Account/General/Mail").InnerText

            For i As Integer = 1 To Settings.MaximumCharacters
                With _Characters(i)
                    _Characters(i).Name = XmlDoc.SelectSingleNode("/Account/Character[" & i & "]/Name").InnerText
                    _Characters(i).Level = XmlDoc.SelectSingleNode("/Account/Character[" & i & "]/Level").InnerText
                    _Characters(i).Sex = XmlDoc.SelectSingleNode("/Account/Character[" & i & "]/Sex").InnerText
                    _Characters(i).Classe = XmlDoc.SelectSingleNode("/Account/Character[" & i & "]/Classe").InnerText
                    _Characters(i).PK = XmlDoc.SelectSingleNode("/Account/Character[" & i & "]/PK").InnerText
                End With
            Next

        Catch ex As Exception
            WriteAccount()
            LoadAccount(Login)
        End Try

    End Sub

    Public Sub WriteAccount()

        Try

            Dim Contents As String = String.Empty

            Contents &= "<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>" & vbCrLf
            Contents &= "<Account>" & vbCrLf
            Contents &= "<General>" & vbCrLf
            Contents &= "   <Login>" & _Login & "</Login>" & vbCrLf
            Contents &= "   <Password>" & _Password & "</Password>" & vbCrLf
            Contents &= "   <Mail>" & _Mail & "</Mail>" & vbCrLf
            Contents &= "   <AddressIP>" & _AddressIP & "</AddressIP>" & vbCrLf
            Contents &= "   <Grade>0</Grade>" & vbCrLf
            Contents &= "</General>" & vbCrLf

            For i As Integer = 1 To Settings.MaximumCharacters

                With _Characters(i)
                    Contents &= "<Character>" & vbCrLf
                    Contents &= "   <Name>" & .Name & "</Name>" & vbCrLf
                    Contents &= "   <Level>" & .Level & "</Level>" & vbCrLf
                    Contents &= "   <Classe>" & .Classe & "</Classe>" & vbCrLf
                    Contents &= "   <Sex>" & .Sex & "</Sex>" & vbCrLf
                    Contents &= "   <Sprite>" & .Sprite & "</Sprite>" & vbCrLf
                    Contents &= "   <PK>" & .PK & "</PK>" & vbCrLf
                    Contents &= "</Character>" & vbCrLf
                End With

            Next

            Contents &= "</Account>" & vbCrLf

            File.WriteAllText(AccountsPath & "" & _Login.Trim & Settings.DocsFormat, Contents)

        Catch ex As Exception
            AddText(ex.ToString, ConsoleColor.Red)
        End Try

    End Sub

    Public Shared Function AccountExist(ByVal Login As String) As Boolean

        If File.Exists(AccountsPath & Login.Trim & Settings.DocsFormat) Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Shared Function GoodPassword(ByVal Login As String, ByVal Password As String) As Boolean
        If Password.Trim.ToUpper.GetHashCode = ReadXml("/Account/General/Password", AccountsPath & Login.Trim & Settings.DocsFormat).Trim.ToUpper.GetHashCode Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Sub CreateNewAccount(ByVal Index As String, ByVal Login As String, ByVal Password As String, ByVal Mail As String)
        With Accounts(Index)
            .Login = Login.Trim
            .Password = Password.Trim
            .Mail = Mail.Trim
            .AddressIP = Clients(Index).AddressIP
            .Grade = Grades.Player
            .WriteAccount()
        End With
    End Sub

    Public Sub CreateCharacter(ByVal Name As String, ByVal Sex As Integer, ByVal Classe As Integer, ByVal slot As Integer)

        _Characters(slot).Name = Name.Trim
        _Characters(slot).Sex = Sex
        _Characters(slot).Classe = Classe

        With Classes(_Characters(slot).Classe)

            _Characters(slot).Stength = .Stength
            _Characters(slot).Defense = .Defense
            _Characters(slot).Agility = .Agility
            _Characters(slot).Magic = .Magic
            _Characters(slot).Heal = .Heal
            _Characters(slot).Mana = .Mana
            _Characters(slot).Special = .Special

            If _Characters(slot).Sex = Genders.Man Then
                _Characters(slot).Sprite = .ManSprite
            ElseIf _Characters(slot).Sex = Genders.Woman Then
                _Characters(slot).Sprite = .WomanSprite
            End If

            _Characters(slot).NumMap = .StartMap
            _Characters(slot).PosX = .StartX
            _Characters(slot).PosY = .StartY

        End With

        WriteAccount()

    End Sub

End Class
